package com.ramnight.unify.boot.s3;

import java.net.URL;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions;
import com.ramnight.unify.kit.exception.UnifyException;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
public class UnifyS3Configuration {
	@Data
	@Configuration
	@ConfigurationProperties(prefix = "unify.s3")
	public static class S3Yml {
		private String accessKey;
		private String secretKey;
		private String endpoint;
	}

	@Bean
	@ConditionalOnProperty("unify.s3.endpoint")
	public AmazonS3 amazonS3(S3Yml s3Yml) {
		try {
			URL endpointUrl = new URL(s3Yml.getEndpoint());
			String protocol = endpointUrl.getProtocol();

			ClientConfiguration clientConfig = new ClientConfiguration();
			clientConfig.setSignerOverride("S3SignerType");
			clientConfig.setProtocol(Protocol.valueOf(protocol.toUpperCase()));

			// 禁用证书检查，避免https自签证书校验失败
			System.setProperty("com.amazonaws.sdk.disableCertChecking", "true");
			// 屏蔽 AWS 的 MD5 校验，避免校验导致的下载抛出异常问题
			System.setProperty("com.amazonaws.services.s3.disableGetObjectMD5Validation", "true");
			AWSCredentials awsCredentials = new BasicAWSCredentials(s3Yml.getAccessKey(), s3Yml.getSecretKey());
			// 创建 S3Client 实例
			AmazonS3 s3client = new AmazonS3Client(awsCredentials, clientConfig);
			s3client.setEndpoint(endpointUrl.getHost() + ":" + endpointUrl.getPort());
			s3client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build());
			return s3client;
		} catch (Exception e) {
			throw new UnifyException(e);
		}
	}

	// public AmazonS3 amazonS3(S3Yml s3Yml) {
	// AWSCredentials credentials = new BasicAWSCredentials(s3Yml.getAccessKey(),
	// s3Yml.getSecretKey());
	// AWSCredentialsProvider credentialsProvider = new
	// AWSStaticCredentialsProvider(credentials);
	// AwsClientBuilder.EndpointConfiguration endpointConfig = new
	// AwsClientBuilder.EndpointConfiguration(s3Yml.getEndpoint(), "");
	// ClientConfiguration config = new ClientConfiguration();
	// config.setSignerOverride("S3SignerType");
	// config.setProtocol(Protocol.HTTP);
	// // // 减少交互，提高性能
	// // config.withUseExpectContinue(false);
	// // config.disableSocketProxy();
	// AmazonS3 amazonS3 =
	// AmazonS3Client.builder().withEndpointConfiguration(endpointConfig).withClientConfiguration(config).withCredentials(credentialsProvider).disableChunkedEncoding().build();
	// return amazonS3;
	// }

	@Bean
	@ConditionalOnBean(AmazonS3.class)
	public UnifyS3 s3(AmazonS3 amazonS3) {
		return new UnifyS3(amazonS3);
	}
}
